﻿using System;
using System.Globalization;

namespace ZSW.Framework.Utils
{
    public static class ZSWFUtil_Date
    {
        /// <summary>
        /// 当前时间戳
        /// </summary>
        /// <param name="zone"></param>
        /// <returns></returns>
        public static long CurrentSecondsTimestamp(int zone = 8)
        {
            DateTimeOffset dateTimeOffset = DateTimeOffset.Now;
            dateTimeOffset = dateTimeOffset.AddHours(zone);
            return dateTimeOffset.ToUnixTimeSeconds();
        }

        /// <summary>
        /// 当前毫秒级时间戳
        /// </summary>
        /// <param name="zone"></param>
        /// <returns></returns>
        public static long CurrentMillisecondsTimestamp(int zone = 8)
        {
            DateTimeOffset dateTimeOffset = DateTimeOffset.Now;
            dateTimeOffset = dateTimeOffset.AddHours(zone);
            return dateTimeOffset.ToUnixTimeMilliseconds();
        }

        /// <summary>
        /// 获取小时级时间戳
        /// </summary>
        /// <param name="zone"></param>
        /// <returns></returns>
        public static long CurrentHoursTimestamp(int zone = 8)
        {
            long timestamp = CurrentSecondsTimestamp(zone);
            return timestamp / 3600;
        }
        
        /// <summary>
        /// 当前时间
        /// </summary>
        /// <param name="zone"></param>
        /// <returns></returns>
        public static string CurrentFormatTime(int zone = 8)
        {
            long timestamp = CurrentSecondsTimestamp(zone);
            return ZSWFUtil_Date.ToTimeFormat(timestamp);
        }

        /// <summary>
        /// 当前时间
        /// </summary>
        /// <param name="zone"></param>
        /// <returns></returns>
        public static DateTime CurrentDateTime(int zone = 8)
        {
            long     timestamp = CurrentSecondsTimestamp(zone);
            DateTime dateTime  = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp);
            return dateTime;
        }

        /// <summary>
        /// 时间戳转格式化时间
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static string ToTimeFormat(long timestamp)
        {
            DateTime dateTime = DateTime.UnixEpoch.AddSeconds(timestamp);
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 格式时间转时间戳
        /// </summary>
        /// <param name="timeformat"></param>
        /// <returns></returns>
        public static long ToSecondsTimestamp(string timeformat)
        {
            DateTime dateTime  = DateTime.ParseExact(timeformat, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
            long     timestamp = (long)dateTime.Subtract(DateTime.UnixEpoch).TotalSeconds;
            return timestamp;
        }

        /// <summary>
        /// GMT2格式时间转DateTime
        /// </summary>
        /// <param name="gmt"></param>
        /// <returns></returns>
        public static DateTime GMT2Local(string gmt)
        {
            DateTime dt = DateTime.MinValue;
            try
            {
                string pattern = "";
                if (gmt.IndexOf("+0") != -1)
                {
                    gmt = gmt.Replace("GMT", "");
                    pattern = "ddd, dd MMM yyyy HH':'mm':'ss zzz";
                }
                if (gmt.ToUpper().IndexOf("GMT") != -1)
                {
                    pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
                }
                if (pattern != "")
                {
                    dt = DateTime.ParseExact(gmt, pattern, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal);
                    dt = dt.ToLocalTime();
                }
                else
                {
                    dt = Convert.ToDateTime(gmt);
                }
            }
            catch
            {
            }
            return dt;
        }
    }
}